#############################################
# Wordpress Connector and Offline Publisher
# 
# author: andreas del galdo
# mail:   andreas.delgaldo@gmx.net
#
#############################################

import sys, os
import wordpresslib

def openWordpress(name,url,username,password):
	try:
		print "name=" + name + " url=" + url + " username=" + username + " password=" + password
		#completing url with xml-rpc connector
		url = url + "/xmlrpc.php"
	
		#creating new wordpress handler
		wpHandler = wordpresslib.WordPressClient(url,username,password)
		wpHandler.selectBlog(0)
		return wpHandler
	except:
		print sys.exc_info()[0]
		return False
	
def publishPost(handler,title,description,category):
	try:
		#creating new post
		post = wordpresslib.WordPressPost()
		post.title = title
		post.description = description
		if (category != None):
			post.categories = (handler.getCategoryIdFromName(category),)
			
		postId = handler.newPost(post,True)
		return postId
	except:
		return False

def getCategories(handler):
	try:
		#getting a list of categories
		tupleOfCategories = tuple(handler.getCategoryList())
		categories = dict()
		for i in range(len(tupleOfCategories)):
			categories[i] = tupleOfCategories[i].name
		
		return categories
	except:
		return False
